package com.richesup.common.oss.service.impl;

import com.richesup.common.oss.utils.Common;
import com.richesup.common.oss.common.CodeMessage;
import com.richesup.common.oss.common.OSSException;
import com.richesup.common.oss.common.ResultEntity;
import com.richesup.common.oss.dao.FileLogMapper;
import com.richesup.common.oss.dao.FileMapper;
import com.richesup.common.oss.entity.FileLog;
import com.richesup.common.oss.service.IFileUpload;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @Auther: yuhong
 * @Date: 2018-6-24 19:13
 * @Description:
 */
@Service("FileUpload")
public class FileUpload implements IFileUpload{

    @Autowired
    FileMapper fileMapper;
    @Autowired
    FileLogMapper fileLogMapper;

    private final static Logger logger= LogManager.getLogger(FileUpload.class);


    @Override
    public String Upload(MultipartFile multipartFile,String path){

        if (multipartFile.isEmpty())  throw new OSSException(ResultEntity.error(CodeMessage.PARAMETER_ISNULL));

        String strPrefix=multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")+1);

        if (!strPrefix.toLowerCase().equals("png") && !strPrefix.toLowerCase().equals("jpg") && !strPrefix.toLowerCase().equals("gif")){
            throw new OSSException(ResultEntity.error(CodeMessage.IMAGE_TYPEEXCEPTION));
        }

        String strFileName=com.richesup.common.oss.utils.File.SaveFile(multipartFile,path);

        if(strFileName==null){
            throw new OSSException(ResultEntity.error(CodeMessage.IMAGE_SAVE_FILE_EXCEPTION));
        }

        return strFileName;


    }

    @Override
    @Transactional
    public String SaveData(String app_id, String path, String file_name) {
        //存数据库
        //File表存储

        String strFileID=null;
        try {
            com.richesup.common.oss.entity.File file1 = new com.richesup.common.oss.entity.File();
            file1.setFile_id(Common.GetUUID());
            file1.setApp_id(app_id);
            file1.setCreate_time(new Date());
            file1.setUpdate_time(new Date());
            file1.setFile_path(path);
            file1.setFile_name(file_name);
            fileMapper.insert(file1);
            strFileID=file1.getFile_id();

            //FileLog表存储
            FileLog fileLog = new FileLog();
            fileLog.setDescription("");
            fileLog.setFile_id(file1.getFile_id());
            fileLog.setFile_log_id(Common.GetUUID());
            fileLog.setFile_name(file_name);
            fileLog.setCreate_time(new Date());
            fileLog.setUpdate_time(new Date());
            fileLogMapper.insert(fileLog);
        }catch (Exception ex){
            logger.error(ex.getStackTrace().toString());
            throw new OSSException(ResultEntity.error(CodeMessage.IMAGE_SAVE_DATABASE_EXCEPTION));
        }

        return strFileID;
    }
}
